/* 
 * Shuriken is the legal property of its developers. Please refer to the
 * COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Text;
using Shuriken.Core.Store;
using Shuriken.Core.Shell;
using Shuriken.Base; 

namespace Shuriken.Core.Store.Scanners
{
    public class ControlPanelScanner : IScanner
    {
        private List<ControlPanelItem> itemlist; 

        public ControlPanelScanner()
        {
            itemlist = new List<ControlPanelItem>();
        }

        /// <summary>
        /// Scanning all control panel items
        /// </summary>
        /// <returns>
        /// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/programmersguide/shell_adv/conpanel.asp
        /// </returns>
        public List<Shell32.FolderItem2> Scan()
        {
            List<Shell32.FolderItem2> result = new List<Shell32.FolderItem2>();            

            Shell32.FolderItems items;            
            items = Utils.ShellUtils.Shell().NameSpace(Utils.ShellFolderSpecialConstants.ssfCONTROLS).Items();

            for (int i = 0; i < items.Count; i++)
            {
                Shell32.FolderItem2 item = (Shell32.FolderItem2)items.Item(i);                       
                result.Add(item);
            }

            return result;
        }

        /// <summary>
        /// Building an image list of control panel items 
        /// </summary>
        /// <returns>list of icon indexes of cpl items</returns>
        private Dictionary<string, int> BuidImageList()
        {
            Shuriken.Core.Shell.ShellManager manager = Shuriken.Core.Shell.ShellManager.Instance;

            List<ShellItem> cpList = manager.GetShellItemsList(ShellAPI.CSIDL.CSIDL_CONTROLS, false);

            Dictionary<string, int> imgDictionary = new Dictionary<string,int>();

            foreach (ShellItem cpl in cpList)
            {
                if (!imgDictionary.ContainsKey(cpl.DisplayName))
                {
                    imgDictionary.Add(cpl.DisplayName, cpl.IconIndex);
                }                
            }

            return imgDictionary;
        }

        #region IScanner Members

        /// <summary>
        /// List of items in this scanner
        /// </summary>
        public List<ISubject> Items
        {
            get 
            {
                return itemlist.ConvertAll<ISubject>(new Converter<ControlPanelItem, ISubject>(delegate(ControlPanelItem p) { return (ISubject)p;  }));
                // return itemlist.ConvertAll<ISubject>(new Converter<ControlPanelItem,ISubject>( delegate(ControlPanelItem p) { return (ISubject)p; });
            }
        }

        /// <summary>
        /// Scane the items
        /// </summary>
        public void Update()
        {
            List<Shell32.FolderItem2> list = Scan();
            Dictionary<string, int> images = BuidImageList();
            itemlist.Clear();

            // Adding to items to the store 
            for (int loop = 0; loop < list.Count; loop++)
            {
                try
                {
                    Shell32.FolderItem2 fItem = (Shell32.FolderItem2)list[loop];
                    ControlPanelItem cpl = new ControlPanelItem(fItem);

                    if (images.ContainsKey(cpl.Name))
                    {
                        string img_key = ImageStore.AddSystemImageListIndex(images[cpl.Name]);
                        cpl.Icon = img_key;

                        itemlist.Add(cpl);
                    }
                }
                catch
                {
                    continue;
                }
            }
        }

        #endregion

        #region IObject Members

        public string Name
        {
            get { return "Control Panel"; }
        }

        public string Description
        {
            get { return "Scanner for scanning control panel items."; }
        }

        public string Icon
        {
            get
            {
                return string.Empty;
            }
            set
            {
                
            }
        }

        #endregion
    }
}
